package com.web.portal.validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.web.portal.entity.Admin;
import com.web.portal.service.AdminService;
import com.web.portal.service.FacultyService;

@Component
public class LoginValidator implements Validator {

	@Autowired
	AdminService adminService;
	

	public boolean supports(Class<?> customClass) {
		return Admin.class.isAssignableFrom(customClass);
	}

	public void validate(Object admin, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
				"required.userName", "Username is required!");

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
				"required.password", "Password is required!");
		
		
		// TODO Auto-generated method stub
Admin administrator = (Admin) admin;
		
		if (!errors.hasErrors()) {
			if (!adminService.isUserNamePasswordValid(
					administrator.getUserName(), administrator.getPassword())) {
				administrator.setUserName("");
				administrator.setPassword("");
				errors.rejectValue("userName", "notmatch.Credentials",
						"You entered incorrect username or password! ");

			}
		}
	}

}
